//
//  SNHomePageDetailChartView.m
//  project
//
//  Created by 黄建华 on 2017/11/25.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNFoodChartView.h"

//#define SPACEWidth 1.0 * floor(self.width - 10)/3.2/48
//#define WIDTH 2.2 * floor(self.width - 10)/3.2/38

@implementation SNFoodChartView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self performSelector:@selector(createChartView) withObject:nil afterDelay:IOSINITWAITTIME];
    }
    return self;
}

- (void)createChartView {
    self.backgroundColor = [UIColor whiteColor];
}

- (void)UpdateViewWithType:(SelectDateType)type withArray:(NSArray *)dataArray withArray:(NSArray *)calorArray
{
    [self loadBackgroundLayer];
    //数据加载
    NSMutableArray *array  = [NSMutableArray array];
    int            allStep = 0;
    if (type == 0) {
        array = [NSMutableArray arrayWithArray:dataArray];
    } else if (type == 1) {
        array = [NSMutableArray arrayWithArray:dataArray];
    } else if (type == 2) {
        array = [NSMutableArray arrayWithArray:dataArray];
    } else if (type == 3) {
        array = [NSMutableArray arrayWithArray:dataArray];
    }
    
    if (type == SelectDateDay) {
        SportModel *model = [SportModel getSportModelFromDBWith:SN_PublicClass.selectDate];
        allStep = model.step;
    } else {
        for (NSString *stepS in array) {
            allStep += stepS.intValue;
        }
    }
    
    _selectDateLabel               = [[SNCustomLabel alloc] initWithFrame:CGRectMake(40, 10, 220, 20)];
    _selectDateLabel.textAlignment = NSTextAlignmentLeft;
    _selectDateLabel.textColor     = SNCOLORBLACK;
    _selectDateLabel.font          = SNSFONT(14.0);
    [_chartView addSubview:_selectDateLabel];
    
    _unitLabel               = [[SNCustomLabel alloc] initWithFrame:CGRectMake(10, self.height - 30, HHHWIDTH - 20, 15)];
    _unitLabel.textColor     = SNCOLORGRAY;
    _unitLabel.font          = SNFONT(10.0);
    _unitLabel.textAlignment = NSTextAlignmentLeft;
    [_chartView addSubview:_unitLabel];
    
    UIView *infoView1 = [[UIView alloc] init];
    infoView1.frame = SNFRAME(self.width - 110, 5, 12, 12);
    infoView1.backgroundColor = [UIColor blackColor];
    [_chartView addSubview:infoView1];
    
    UILabel *infoLabel1 = [UILabel simpleWithRect:SNFRAME(infoView1.right + 5, infoView1.top,self.width - 18 -infoView1.right, 12) withAlignment:NSTextAlignmentLeft withFontSize:9.5 withText:SN_Text(@"运动消耗") withTextColor:[UIColor blackColor]];
    [_chartView addSubview:infoLabel1];
    
    UIView *infoView2 = [[UIView alloc] init];
    infoView2.frame = SNFRAME(infoView1.left, infoView1.bottom + 5, 12, 12);
    infoView2.backgroundColor = FOODCOLOR;
    [_chartView addSubview:infoView2];
    
    UILabel *infoLabel2 = [UILabel simpleWithRect:SNFRAME(infoView2.right + 5, infoView2.top,infoLabel1.width, 12) withAlignment:NSTextAlignmentLeft withFontSize:9.5 withText:SN_Text(@"卡路里摄入") withTextColor:[UIColor blackColor]];
    [_chartView addSubview:infoLabel2];
    
    if (type == 0) {
        _selectDateLabel.text = @"";
    } else {
        _selectDateLabel.text = [SN_PublicClass showDateWithType:type];
    }
    
    // 求出运动/摄入 消耗最大值 作为max
    NSInteger maxValue = 0;
    
    NSInteger sportMax = [self max:array];
    NSInteger calorMax = [self max:calorArray];
    if (sportMax > calorMax) {
        maxValue = sportMax;
    }
    else
    {
        maxValue = calorMax;
    }
    
    float   Origin_x      = 50;
    float   Origin_xRight = 10;
    CGFloat Origin_height = self.height - 40;
    CGFloat offsetY       = FitScreenNumber(40, 40, 40, 40, 40);
    
    CGFloat lineH = (self.height - (15 + 30 + offsetY)) * 1;
    
    UIView *lineGray = [[UIView alloc] init];
    lineGray.backgroundColor = SNCOLORWITHHEX(0xd7d8d9);
    lineGray.frame           = SNFRAME(Origin_x-10, self.height - 40, HHHWIDTH - Origin_x, 1);
    [_chartView addSubview:lineGray];
    
    UIView *lineGray2 = [[UIView alloc] init];
    lineGray2.backgroundColor = SNCOLORWITHHEX(0xd7d8d9);
    lineGray2.frame           = SNFRAME(Origin_x-10, self.height - 40 -lineH, 1, lineH);
    [_chartView addSubview:lineGray2];
    
    NSMutableArray *arrayYvalue = [NSMutableArray array];
    NSInteger aveValue = maxValue /4;
    if (maxValue <=4) {
        if (maxValue >0) {
            [arrayYvalue addObject:StrByInt(maxValue)];
        }
        else
        {
            [arrayYvalue addObject:@""];
        }
        [arrayYvalue addObject:@""];
        [arrayYvalue addObject:@""];
        [arrayYvalue addObject:@""];
        [arrayYvalue addObject:@"0"];
    }
    else
    {    [arrayYvalue addObject:StrByInt(maxValue)];
         [arrayYvalue addObject:StrByInt(aveValue*3)];
         [arrayYvalue addObject:StrByInt(aveValue*2)];
         [arrayYvalue addObject:StrByInt(aveValue*1)];
         [arrayYvalue addObject:@"0"];
    }
    
    for (int i = 1; i < 6; i++) {
        UIView *line = [[UIView alloc] init];
        line.frame           = SNFRAME(lineGray.left, lineH *0.25*i, lineGray.width, 1);
        line.backgroundColor = SNCOLORWITHHEX(0xd7d8d9);
        line.alpha           = 0.33;
        [_chartView addSubview:line];
        
        UILabel *label = [UILabel simpleWithRect:SNFRAME(lineGray.left - Origin_x-1, lineH *0.25*i -10,Origin_x-5,15) withAlignment:NSTextAlignmentRight withFontSize:10.0 withText:arrayYvalue[i-1] withTextColor:[UIColor blackColor]];
        [_chartView addSubview:label];
    }
    
    for (int i = 0; i < array.count; i++) {
        CGFloat value = [[array objectAtIndex:i] floatValue];
        if (value > maxValue) {
            value = maxValue;
        }
        
        CGFloat height = 0.0;
        CGFloat h      = (self.height - (25 + 30 + offsetY)) * (value / maxValue);
        if (isnan(h)) {
            height = 0.0;
        } else {
            height = h;
        }
        
        
        CGFloat calorValue = [[calorArray objectAtIndex:i]floatValue];
        if (calorValue >maxValue) {
            calorValue = maxValue;
        }
        
        CGFloat height2 =0.0;
        CGFloat h2 = (self.height - (25 + 30 + offsetY)) * (calorValue / maxValue);
        if (isnan(h2)) {
            height2 = 0.0;
        } else {
            height2 = h2;
        }
        
        CGFloat chartWidth = 0;
        CGFloat chartSpace = 0;
        if (type == 0) {
            chartWidth = 2.5;
            chartSpace = (self.width - Origin_x -Origin_xRight)/48;
        } else if (type == 1) {
            chartSpace = (self.width - Origin_x -Origin_xRight)/7 + 5;
            chartWidth = 10;
        } else if (type == 2) {
            chartSpace = (self.width - Origin_x -Origin_xRight)/31;
            chartWidth = 6;
        } else if (type == 3) {
            chartSpace = (self.width - Origin_x -Origin_xRight)/12 + 1.5;
            chartWidth = 10;
        }
        
        CGPoint point01 = CGPointMake(i * chartSpace + Origin_x, Origin_height-height);
//        CGPoint point02 = CGPointMake((i) * chartSpace + Origin_x +chartWidth, Origin_height-height);
        CGPoint point011 = CGPointMake(i * chartSpace + Origin_x, Origin_height - height2);
        
        // 运动消耗 间隔1
        CGFloat width = (chartWidth - 1)/2.0;
        
        UIView *chartViewSport = [[UIView alloc]init];
        chartViewSport.frame = SNFRAME(point01.x, point01.y + height, width, height);
        [chartViewSport addRectCorner:UIRectCornerTopLeft|UIRectCornerTopRight withSize:CGSizeMake(width, width)];
        chartViewSport.backgroundColor = [UIColor blackColor];
        [_chartView addSubview:chartViewSport];
        
        // 摄入消耗
        UIView *chartViewFood = [[UIView alloc]init];
        chartViewFood.frame = SNFRAME(chartViewSport.right +1, point011.y + height, width, height2);
        [chartViewFood addRectCorner:UIRectCornerTopLeft|UIRectCornerTopRight withSize:CGSizeMake(width, width)];
        chartViewFood.backgroundColor = FOODCOLOR;
        [_chartView addSubview:chartViewFood];
        
        // 底部白色背景覆盖超出的子视图
        UIView *bgView = [[UIView alloc] init];
        bgView.frame = SNFRAME(point01.x, point01.y + height,chartWidth+5,self.height);
        bgView.backgroundColor = [UIColor whiteColor];
        if (value > 0.0) {
           [_chartView addSubview:bgView];
        }
        
        [UIView animateWithDuration:SNACTIONTIME animations:^{
            chartViewSport.frame = SNFRAME(point01.x, point01.y, width, height);
            chartViewFood.frame = SNFRAME(chartViewSport.right +2, point011.y, width, height2);
        } completion:^(BOOL finished) {
            bgView.hidden = YES;
        }];
        
        CGFloat centerX = point01.x + chartWidth/2.0;
        
        if (type == 0) {
            NSArray *weekArray = [[NSArray alloc] initWithObjects:@"00:00", @"04:00", @"08:00", @"12:00", @"16:00", @"20:00", @"23:59", nil];
            
            SNCustomLabel *label = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
            label.center        = CGPointMake(centerX + 4.0, self.height - 25);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor     = SNCOLORBLACK;
            label.font          = SNSFONT(9.0);
            [_chartView addSubview:label];
            if (i % 8 == 0) {
                label.text = [weekArray objectAtIndex:i/8];
            }
            
            if (i == 47) {
                label.text   = [weekArray objectAtIndex:6];
                label.center = CGPointMake(point01.x -   4.0, self.height - 25);
            }
        }
        
        if (type == 1) {
            NSArray       *weekArray = [[NSArray alloc] initWithObjects:SN_Text(@"周日"), SN_Text(@"周一"), SN_Text(@"周二"), SN_Text(@"周三"), SN_Text(@"周四"), SN_Text(@"周五"), SN_Text(@"周六"), nil];
            SNCustomLabel *label     = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
            label.center        = CGPointMake(centerX, self.height - 25);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor     = SNCOLORBLACK;
            label.font          = SNSFONT(10.0);
            label.text          = [weekArray objectAtIndex:i];
            [_chartView addSubview:label];
        }
        if (type == 2) {
            SNCustomLabel *label = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
            label.center        = CGPointMake(centerX, self.height - 25);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor     = SNCOLORBLACK;
            label.font          = SNSFONT(10.0);
            [_chartView addSubview:label];
            if (i == 0 || i == 4 || i == 9 || i == 14 || i == 19 || i == 24 || i == 29) {
                label.text = StrByInt(i+1);
            } else {
                label.text = @".";
            }
        }
        if (type == 3) {
            SNCustomLabel *label = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
            label.center        = CGPointMake(centerX, self.height - 25);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor     = SNCOLORBLACK;
            label.font          = SNSFONT(10.0);
            label.text          = StrByInt(i+1);;
            [_chartView addSubview:label];
        }
    }
}

- (void)loadBackgroundLayer {
    if (_chartView) {
        [_chartView removeFromSuperview];
        _chartView = nil;
    }
    
    _chartView                 = [[UIView alloc] init];
    _chartView.backgroundColor = [UIColor whiteColor];
    _chartView.frame           = CGRectMake(0, 0, self.width, self.height);
    _chartView.clipsToBounds = YES;
    _chartView.layer.masksToBounds = YES;
    [self addSubview:_chartView];
    
    if (_backGroundLayer) {
        [_backGroundLayer removeFromSuperlayer];
        _backGroundLayer = nil;
    }
    _backGroundLayer                 = [CALayer layer];
    _backGroundLayer.backgroundColor = [UIColor clearColor].CGColor;
    _backGroundLayer.bounds          = CGRectMake(0, 0, self.width, self.height);
    _backGroundLayer.position        = CGPointMake(self.center.x, self.height / 2);
    _backGroundLayer.masksToBounds = YES;
    [_chartView.layer addSublayer:_backGroundLayer];
}

- (NSInteger)max:(NSArray *)Array {
    if (Array.count == 0) {
        return 0;
    }
    
    NSInteger max = [[Array objectAtIndex:0] integerValue];
    
    for (int i = 0; i < Array.count; i++) {
        if ([[Array objectAtIndex:i] integerValue] > max) {
            max = [[Array objectAtIndex:i] integerValue];
        }
    }
    
    return max;
}

- (NSInteger)min:(NSArray *)Array {
    if (Array.count == 0) {
        return 0;
    }
    
    NSInteger min = [[Array objectAtIndex:0] integerValue];
    
    for (int i = 0; i < Array.count; i++) {
        if ([[Array objectAtIndex:i] integerValue] < min) {
            min = [[Array objectAtIndex:i] integerValue];
        }
    }
    return min;
}

@end

